import { CatygoryService } from './catygory.service'
import { Catygory, CatygoryResponse } from './catygory.interface';
import { CreateCatygoryDTO, EditCatygoryDTO } from './category.dto';
import {
    Body,
    Controller,
    Delete,
    Get,
    Param,
    Post
} from '@nestjs/common'

@Controller('category')
export class CatygoryController {
    constructor(private readonly catygoryService: CatygoryService) {}

    @Get('all')
    async findAll() : Promise<CatygoryResponse<Catygory[]>> {
        return {
            code: 200,
            data: await this.catygoryService.findAll(),
            msg: 'success'
        }
    }

    @Get(':_id')
    async find(@Param('_id') _id: string) : Promise<CatygoryResponse<Catygory>> {
        return {
            code: 200,
            data: await this.catygoryService.find(_id),
            msg: 'success'
        }
    }

    @Post('getby')
    async findWhere(@Body() filter?: object) : Promise<CatygoryResponse<Catygory[]>> {
        return {
            code: 200,
            data: await this.catygoryService.findWhere(filter),
            msg: 'success'
        }
    }

    @Post('add')
    async addone(@Body() body: CreateCatygoryDTO) : Promise<CatygoryResponse> {
        await this.catygoryService.addone(body);
        return {
            code: 200,
            msg: 'success'
        }
    }

    @Post('edit')
    async editone(@Body() body: EditCatygoryDTO) : Promise<CatygoryResponse> {
        await this.catygoryService.editone(body._id, body);
        return {
            code: 200,
            msg: 'success'
        }
    }

    @Delete(':_id')
    async deleteone(@Param('_id') _id: string) : Promise<CatygoryResponse> {
        return {
            code: 200,
            data: await this.catygoryService.delete(_id),
            msg: 'success'
        }
    }}
